<?php
namespace User\Controller;

use Zend\View\Model\ViewModel;
use Zend\Mvc\Controller\AbstractActionController;
class UserController extends AbstractActionController
{
    private $userInfoTable;

    public function indexAction(){
        $request = $this->getRequest();
        if($request->isPost()){
            $objs_datas = $this->getUserInfoTable()->fetchAll();
            $datas = array();
            //两行数据变为2个对象
            foreach ($objs_datas as $obj) {
                $datas[] = $obj->getArrayCopy();
            }
            echo json_encode($datas);           //接口：json_encode()
            exit();
        }
        return new ViewModel();
    }

    public function getUserInfoTable()
    {
        if ($this->userInfoTable == null) {
            $sm = $this->getServiceLocator();
            $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
            $resultSetPrototype = new \Zend\Db\ResultSet\ResultSet();
            $tableGateway = new \Zend\Db\TableGateway\TableGateway('user_info', $dbAdapter, null, $resultSetPrototype);
            // $user = new User();
            // $user->exchangeArray($data);
            $this->userInfoTable = new UserInfoTable($tableGateway);
            // $this->userInfoTable = $sm->get('User\Model\UserInfoTable');
        }
        return $this->userInfoTable;
    }
}
